console.log(chrome,'chrome');
var currentTabId;
var bgVars = chrome.extension.getBackgroundPage();
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
    currentTabId = tabs[0].id;
    if(bgVars.mediaList !=undefined && bgVars.mediaList[currentTabId] != undefined){
        for(var i=0;i<bgVars.mediaList[currentTabId].length;i++){
            addMedia(bgVars.mediaList[currentTabId][i]);
        }
    }
});


chrome.runtime.onMessage.addListener(function(data){
    console.log(data);
    if(data.tabId == currentTabId){
        addMedia(data);
    }
});

//addFace
function addMedia(data) {
    if(typeof data != 'undefined' && Object.keys(data).length  > 0){
        if($("#mediaScope").is(":hidden")){
            $("#mediaScope").show();
            $("#tip").hide();
        }
        $('#mediaList').append(generateMediaLi(data.name,data.suffix,data.size,data.url));
    }
}

// function generateMediaLi(name,type,size,url) {
//     if($.isNumeric( size )) size =Math.round(size*Math.pow(10,1)/1024)/Math.pow(10,1) + "MB";
//     var li = '<li>' +
//              '<label class="name">'+name+'</label>' +
//              '<a href="'+url+'" target="_blank" download="'+name+'.'+type+'"><span class="download"><i></i></span></a>' +
//                 '<span class="play"><i></i></span>'+
//                 '<span class="link"><i></i></span>' +
//                 '<span class="size">'+size+'</span>' +
//                 '<span class="format">'+type+'</span>' +
//             '</li>';
//
//      return li;
// }


function generateMediaLi(name,type,size,url) {
    if($.isNumeric( size )) size =Math.round(size*Math.pow(10,1)/1024)/Math.pow(10,1) + "MB";
    var li = '<li>' +
        '<label class="name">'+name+'</label>' +
        '<a href="'+url+'" target="_blank" download="'+name+'.'+type+'"><span class="download"><i></i></span></a>';

    if(isCanPlayMedia(type)){
        li = li + '<span class="play"><i></i></span>';
    }

    li = li + '<span class="link"><i></i></span>' +
        '<span class="size">'+size+'</span>' +
        '<span class="format">'+type+'</span>' +
        '</li>';

    return li;
}

$("#mediaList").on("click",".play",function () {
   var url = $(this).siblings("a").attr("href");
    $(this).parents("li").siblings().hide();
    $("#mediaPlayer").show();
    $("#mediaPlayer").children("video").attr("src",url);
})

$("#mediaPlayer").on("click",".videoCloser",function () {
    $("#mediaList li").fadeIn();
    $("#mediaPlayer").children("video").attr("src","");
    $("#mediaPlayer").hide();
})



$("#mediaList").on("click",".link",function () {
    copyTextToClipboard($(this).siblings("a").attr("href"));
})

function copyTextToClipboard(text) {
    var copyFrom = $('<textarea/>');
    copyFrom.text(text);
    $('body').append(copyFrom);
    copyFrom.select();
    document.execCommand('copy');
    copyFrom.remove();
    showMessage("已复制到粘贴板");
}

function showMessage(text) {
    $("#messageContext").text(text);
    $("#message").fadeIn();
}

$(".modal-closer").on("click",function () {
    $(this).parents(".w3-modal").fadeOut();
    $("#messageContext").text("");
})


var videoSupportType = ["mp3","mp4","aac",
"ogg","webm","wave"]
function isCanPlayMedia(suffix) {
   suffix = suffix.toLowerCase();
   if($.inArray(suffix, videoSupportType) == -1){
      return false;
   }else{
    return true;
   }
}